package com.example.xungeng2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xungeng2.mapper.UserMapper;
import com.example.xungeng2.pojo.User;
import com.example.xungeng2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUserInformation(String username) {
        User user = baseMapper.findByUsername(username);
        return user;
    }

    @Override
    public boolean registerAdminUser(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAuthority(0);
        user.setIsAssigned(0);
        user.setIsDeleted(0);
        //使用findByUsername（）方法，判断userCheck是否为空（在数据库中是否存在）
        User userCheck = baseMapper.findByUsername(user.getUsername());
        if (userCheck == null) {
            user.setAuthority(0);
            //不存在，执行插入操作，注册成功，返回首页面
            return 1 == baseMapper.insert(user);
        } else {
            return false;
        }
    }

    @Override
    public boolean setUserInformation(User user) {
        userService.updateById(user);
        return true;
    }

    @Override
    public boolean pipei(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        queryWrapper.eq(User::getPassword,user.getPassword());
        User user1 = userService.getOne(queryWrapper);
        if(user1 == null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean headerImageUpload(User user, String staticPath) {
        String url = user.getImg();
        //使用findByUrl（）方法，判断userCheck是否为空（在数据库中是否存在）
        User userCheck = userMapper.findByUrl(url);
        if (userCheck == null) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername, user.getUsername());
            User user1 = userService.getOne(queryWrapper);
            if (null != user.getImg()) {
                String oldImg = user.getImg();
                File file = new File(staticPath + "/" + oldImg);
                file.delete();
            }
            //不存在，执行插入url操作
            boolean b = userMapper.updatePhotoUrl(user.getUsername(), user.getImg()) != 0;
            return b;
        } else {
            return false;
        }
    }

    @Override
    public User selectUserByUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        return userService.getOne(queryWrapper);
    }

    @Override
    public String selectUserRealNameById(Integer adminId) {
        String realName = userMapper.selectUserRealName(adminId);
        return realName;
    }

    @Override
    public int selectXGUserStatus(Integer userId) {
        return userMapper.selectXGUserStatus(userId);
    }

    @Override
    public User selectUserById(String valueOf) {
        return userMapper.findUserById(Integer.parseInt(valueOf));
    }

    @Override
    public boolean xgUpdatePhotoUrl(Integer userId, String path) {
        Integer integer = userMapper.xgUpdatePhotoUrl(userId, path);
        return integer==1;
    }

    @Override
    public void changeUserState(String userId) {
        userMapper.changeUserState(Integer.parseInt(userId));
    }

    @Override
    public ArrayList<User> selectAllUsers() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = null;
        ArrayList<User> users = (ArrayList<User>) userMapper.selectList(queryWrapper);
        return users;
    }
}
